package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlTransient;
import valueObject.CourseVO;
import valueObject.NodeVO;
import valueObject.ProfessorVO;

/**
 *
 * @author David
 */
@Entity
@Table(name = "course")
@NamedQueries({
    @NamedQuery(name="CourseByCategory",query="SELECT c FROM Course c WHERE c.categoryidCategory.idCategory = :idc")})
public class Course implements Serializable, JPAEntity<CourseVO> {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idCourse")
    private Integer idCourse;
    private String name;
    @Lob
    @Column(name = "description")
    private String description;
    @Basic(optional = false)
    @Lob
    @Column(name = "image")
    private byte[] image;
    @JoinTable(name = "course_has_professor", joinColumns = {
        @JoinColumn(name = "course_idCourse", referencedColumnName = "idCourse")}, inverseJoinColumns = {
        @JoinColumn(name = "professor_professorUser", referencedColumnName = "professorUser")})
    @ManyToMany
    private Collection<Professor> professorCollection;
    @JoinColumn(name = "category_idCategory", referencedColumnName = "idCategory")
    @ManyToOne(optional = false)
    private Category categoryidCategory;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "courseidCourse")
    private Collection<Node> nodeCollection;

    public Course() {
    }

    public Course(Integer idCourse) {
        this.idCourse = idCourse;
    }

    public Course(Integer idCourse, byte[] image) {
        this.idCourse = idCourse;
        this.image = image;
    }

    public Integer getIdCourse() {
        return idCourse;
    }

    public void setIdCourse(Integer idCourse) {
        this.idCourse = idCourse;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }

    @XmlTransient
    public Collection<Professor> getProfessorCollection() {
        return professorCollection;
    }

    public void setProfessorCollection(Collection<Professor> professorCollection) {
        this.professorCollection = professorCollection;
    }

    public Category getCategoryidCategory() {
        return categoryidCategory;
    }

    public void setCategoryidCategory(Category categoryidCategory) {
        this.categoryidCategory = categoryidCategory;
    }

    @XmlTransient
    public Collection<Node> getNodeCollection() {
        return nodeCollection;
    }

    public void setNodeCollection(Collection<Node> nodeCollection) {
        this.nodeCollection = nodeCollection;
    }

    @Override
    public CourseVO toValueObject() {
        CourseVO courseVO = new CourseVO();
        courseVO.setCategory(categoryidCategory.toValueObject());
        courseVO.setName(name);
        courseVO.setDescription(description);
        courseVO.setId(idCourse);
        courseVO.setImage(image);
        ArrayList<NodeVO> nodeVOs = new ArrayList<NodeVO>();
        /*if(nodeCollection != null){
            for(Node n : nodeCollection){
                nodeVOs.add(n.toValueObject());
            }
        }*/
        courseVO.setNodes(nodeVOs);
        ArrayList<ProfessorVO> professorVOs = new ArrayList<ProfessorVO>();
        /*if(professorCollection != null){
            for(Professor p : professorCollection){
                professorVOs.add(p.toValueObject());
            }
        }*/
        courseVO.setProfessors(professorVOs);
        return courseVO;
    }
    
}